﻿using System.Text;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Eneta.Portal.Common.Dto;
using Eneta.Portal.Common.Repository;
using Microsoft.SharePoint;

namespace Eneta.Portal.Controls
{
    public class BlogsLeftMenuControl : HierarchicalDataBoundControl
    {
        public virtual string MenuClass { get; set; }

        /// <summary>
        /// Renders begin tag of menu element.
        /// </summary>
        /// <param name="writer">HtmlTextWriter that accepts menu control output.</param>
        public override void RenderBeginTag(HtmlTextWriter writer)
        {
            if (string.IsNullOrEmpty(MenuClass))
                writer.Write("<ul>");
            else
                writer.Write("<ul class=\"" + MenuClass + "\">");
        }

        protected override void RenderContents(HtmlTextWriter output)
        {
            EnsureDataBound();

            var source = (SiteMapDataSource)GetDataSource();
            var provider = source.Provider;

            if (provider == null)
            {
                output.Write("&nbsp;");
                return;
            }

            if (provider.RootNode == null)
            {
                output.Write("&nbsp;");
                return;
            }
            if (provider.RootNode.ChildNodes.Count == 0)
            {
                output.Write("&nbsp;");
                return;
            }

            var node = provider.CurrentNode;
            while (true)
            {
                if (node.ParentNode.ParentNode == null)
                    break;
                node = node.ParentNode;
            }

            foreach (SiteMapNode childNode in node.ChildNodes)
            {

                output.Write("<li");
                if (childNode.Equals(provider.CurrentNode))
                    output.Write(" class=\"active\"");
                output.Write("><a href=\"" + childNode.Url + "\">");
                output.Write(childNode.Title);
                output.Write("</a>");

                if (childNode.Title == "Blogid")
                {
                    string blogs = GetApprovedBlogs();
                    if (!string.IsNullOrEmpty(blogs))
                    {
                        output.Write("<ul class=\"subnav\">");
                        output.Write(blogs);
                        output.Write("</ul>");
                    }
                }
                if (childNode.ChildNodes.Count > 0)
                {
                    output.Write("<ul class=\"subnav\">");
                    foreach (SiteMapNode childChildNode in childNode.ChildNodes)
                    {
                        output.Write("<li");
                        if (childChildNode.Equals(provider.CurrentNode))
                            output.Write(" class=\"active\"");
                        output.Write("><a href=\"" + childChildNode.Url + "\">");
                        output.Write(childChildNode.Title);
                        output.Write("</a>");
                        output.Write("</li>");
                    }
                    output.Write("</ul>");
                }

                output.Write("</li>");
            }
        }

        private string GetApprovedBlogs()
        {
            var blogsString = new StringBuilder(); 
            var repository = new BlogRepository(SPContext.Current.Web);
            var blogs = repository.GetApprovedBlogs("Title");
            string blog = string.Empty;
            foreach (BlogDto dto in blogs)
            {
                blog = "<li><a href='{0}'>{1}</a></li>";
                blogsString.Append(string.Format(blog, "/uudised/blogid/Lehed/" + dto.FileName, dto.Title));
            }
            return blogsString.ToString();
        }

        /// <summary>
        /// Renders the closing tag of menu.
        /// </summary>
        /// <param name="writer">HtmlTextWriter that accepts menu control output.</param>
        public override void RenderEndTag(HtmlTextWriter writer)
        {
            writer.Write("</ul>");
        }
    }
}
